#ifndef _DS_POOL_H
#define _DS_POOL_H

#if 0
#include "list.h"

/*
*/

#define DS_POOL_ALLOC_TYPE_EXT 0//�ⲿ���䣬��������̬����
#define DS_POOL_ALLOC_TYPE_PRE 1//Ԥ�ȷ��䣬��������̬����
#define DS_POOL_ALLOC_TYPE_DYN 2//��̬����

typedef struct _ds_pool
{
	unsigned int item_size;
	unsigned int item_cnt;//total count

	unsigned char alloc_type;
	unsigned char learning_rate;//����DS_POOL_ALLOC_TYPE_DYNʱ������
	unsigned int alloc_cnt;//����DS_POOL_ALLOC_TYPE_DYNʱ������
//	unsigned int list_offset;
	unsigned char *buf;//����DS_POOL_ALLOC_TYPE_EXT��DS_POOL_ALLOC_TYPE_PREʱ������

	struct list_head head;
	unsigned int free_cnt;
} ds_pool;

int ds_pool_init(ds_pool *dsp, unsigned int item_size, unsigned int item_cnt, unsigned char alloc_type, unsigned char *buf);
int ds_pool_uninit(ds_pool *dsp);

static inline void *ds_pool_get(ds_pool *dsp)
{
	struct list_head *entry;

	if (dsp->free_cnt)
	{
		dsp->free_cnt--;
		entry = dsp->head.next;
		__list_del(entry->prev, entry->next);
		return entry;
	}

	if (dsp->alloc_type == DS_POOL_ALLOC_TYPE_DYN)
	{
		entry = (struct list_head *)malloc(dsp->item_size);
		if (entry)
		{
			dsp->alloc_cnt++;
			return entry;
		}
	}

	return NULL;
}

static inline int ds_pool_put(ds_pool *dsp, void *ptr)
{
	if (dsp->free_cnt < dsp->item_cnt)
	{
		dsp->free_cnt++;
		list_add((struct list_head *)ptr, &dsp->head);
	}
	else
	{
		free(ptr);
		dsp->alloc_cnt--;
	}

	return 0;
}

static inline int ds_pool_put_list(ds_pool *dsp, struct list_head *head, unsigned int cnt)
{//ĿǰΪ���װ汾��ֻ������listΪ��һ����Ա���Ҳ�֧���Զ��ͷţ�DS_POOL_ALLOC_TYPE_DYN��
	dsp->free_cnt += cnt;
	list_merge(&dsp->head, head);

	return 0;
}

#endif

#endif